package it.cqupt.web.controller.store;

import com.github.pagehelper.PageInfo;
import it.cqupt.domain.store.Company;
import it.cqupt.service.store.CompanyService;
import it.cqupt.service.store.impl.CompanyServiceImpl;
import it.cqupt.utils.BeanUtil;
import it.cqupt.web.controller.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/store/company")
public class CompanyServlet extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = req.getParameter("operation");
        if("list".equals(operation)){
            try {
                this.list(req,resp);
            }catch (Exception e){
                e.printStackTrace();
            }
        }else if("toAdd".equals(operation)){
            try {
                this.toAdd(req,resp);
            }catch (Exception e){
                e.printStackTrace();
            }
        }else if("save".equals(operation)){
            this.save(req,resp);
        }else if("toEdit".equals(operation)){
            this.toEdit(req,resp);
        }else if("edit".equals(operation)){
            this.edit(req,resp);
        }else if("delete".equals(operation)){
            this.delete(req,resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        try {
            Company company = BeanUtil.fillBean(req, Company.class);
            cs.delete(company);
            resp.sendRedirect(req.getContextPath()+"/store/company?operation=list");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) {
        try {
            Company company = BeanUtil.fillBean(req, Company.class, "yyyy-MM-dd");
            cs.update(company);
            resp.sendRedirect(req.getContextPath()+"/store/company?operation=list");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) {
        try{
            String id = req.getParameter("id");
            Company company = cs.findById(id);
            req.setAttribute("company",company);
            req.getRequestDispatcher("/WEB-INF/pages/store/company/update.jsp").forward(req,resp);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) {

        try{
            Company company = BeanUtil.fillBean(req, Company.class, "yyyy-MM-dd");
            cs.save(company);
            resp.sendRedirect(req.getContextPath()+"/store/company?operation=list");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        req.getRequestDispatcher("/WEB-INF/pages/store/company/add.jsp").forward(req,resp);
    }

    private void list(HttpServletRequest request,HttpServletResponse response) throws Exception{
        int page = 1;
        int size = 5;
        if(StringUtils.isNotBlank(request.getParameter("page"))){
            page = Integer.parseInt(request.getParameter("page"));
        }
        if(StringUtils.isNotBlank(request.getParameter("size"))){
            size = Integer.parseInt(request.getParameter("size"));
        }
        PageInfo all = cs.findAll(page, size);
        request.setAttribute("page",all);
        request.getRequestDispatcher("/WEB-INF/pages/store/company/list.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
